﻿using System;
using System.Collections.Generic;
using Best.SmartCms.Dao;
using Best.SmartCms.Domain;
using Best.SmartCms.Dto.Response;
using Best.SmartCms.Dto;
using Best.SmartCms.Dto.Enums;
using Best.SmartCms.Service.BizException;
using Best.SmartCms.Service.Common;
using Best.SmartCms.Utility;

namespace Best.SmartCms.Service
{
    /// <summary>
    /// News服务
    /// </summary>
    public class NewsService : IService
    {
        private readonly NewsDao _newsDao;
        private readonly FileService _fileService;
        private readonly ChannelDao _channelDao;
        private readonly WorkContext _workContext;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mySqlDbContext"></param>
        /// <param name="fileService"></param>
        /// <param name="workContext"></param>
        public NewsService(MySqlDbContext mySqlDbContext, FileService fileService, WorkContext workContext)
        {
            _newsDao = new NewsDao(mySqlDbContext);
            _channelDao = new ChannelDao(mySqlDbContext);
            _workContext = workContext;
            _fileService = fileService;
        }

        

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagedResponse<NewsListDto> PageList(NewsListQueryDto query)
        {
            var list = _newsDao.PageList(query, out var totalCount, out var totalPages);
            var pageList = TinyMapperExtensions.Map<List<News>, List<NewsListDto>>(list);
            foreach (var item in pageList)
            {
                var channel = _channelDao.GetSingle(item.ChannelId);
                item.ChannelName = channel?.Name;
                item.ControllerName = channel?.ControllerName;
                var images = _fileService.GetFiles(item.Id, AttachmentSourceType.News.GetHashCode());
                if (images.Count > 0)
                {
                    item.ImageUrl = string.IsNullOrEmpty(images[0].ThumbUrl) ? images[0].Url : images[0].ThumbUrl;
                }
            }

            return new PagedResponse<NewsListDto>(pageList, query.PageIndex,query.PageSize,totalCount,totalPages);
        }

        

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        public void Create(NewsEditDto dto)
        {
            var entity = TinyMapperExtensions.Map<NewsEditDto, News>(dto);
            entity.CreatedTime = DateTime.Now;
            entity.CreatorId = _workContext.UserId;
            entity.CreatorName = _workContext.UserName;
            entity.UpdatorId = _workContext.UserId;
            entity.UpdatorName = _workContext.UserName;
            entity.UpdatedTime = DateTime.Now;
            _newsDao.Add(entity);
            _fileService.SaveFiles(dto.ImageList, entity.Id, AttachmentSourceType.News.GetHashCode());
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        public void Edit(NewsEditDto dto)
        {
            var entity = _newsDao.GetSingle(dto.Id);
            if (entity == null)
            {
                throw new ParameterNullException($"未找到{dto.Id}的数据");
            }

            TinyMapperExtensions.Map(dto, entity);
            
            entity.UpdatorId = _workContext.UserId;
            entity.UpdatorName = _workContext.UserName;
            entity.UpdatedTime = DateTime.Now;
            _newsDao.Update(entity);
            _fileService.SaveFiles(dto.ImageList, entity.Id, AttachmentSourceType.News.GetHashCode());
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public NewsEditDto Info(long id)
        {
            var entity = _newsDao.GetSingle(id);
            var images = _fileService.GetFiles(id, AttachmentSourceType.News.GetHashCode());
            var dto = TinyMapperExtensions.Map<News, NewsEditDto>(entity);
            dto.ImageList = images;
            return dto;
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public NewsDto Detail(long id)
        {
            var entity = _newsDao.GetSingle(id);
            var dto = TinyMapperExtensions.Map<News, NewsDto>(entity);
            var channel = _channelDao.GetSingle(id);
            dto.ChannelName = channel?.Name;
            dto.ControllerName = channel?.ControllerName;
            var images = _fileService.GetFiles(id, AttachmentSourceType.News.GetHashCode());
            if (images.Count > 0)
            {
                dto.ImageUrl = string.IsNullOrEmpty(images[0].ThumbUrl) ? images[0].Url : images[0].ThumbUrl;
            }

            return dto;
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(long id)
        {
            var entity = _newsDao.GetSingle(id);
            if (entity == null)
            {
                throw new ParameterNullException($"未找到{id}的数据");
            }

            _newsDao.Delete(entity);
        }
    }
}
